﻿using Dynamics.NAV.Metro.Common;
using Dynamics.NAV.Metro.Controls.Settings;
using Dynamics.NAV.Metro.Data;
using Okra;
using Okra.Navigation;
using Okra.Services;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Composition.Convention;
using System.Composition.Hosting;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
using Windows.UI.ApplicationSettings;

namespace Dynamics.NAV.Metro
{
    public class AppBootstrapper : OkraBootstrapper
    {
        [Import]
        public ILifetimeManager LifetimeManager { get; set; }

        protected override void SetupServices()
        {
            base.SetupServices();

            NavigationManager.NavigationStorageType = NavigationStorageType.Local;

            LifetimeAwareService ltService = new LifetimeAwareService();
            LifetimeManager.Register(ltService);
        }

        protected override ContainerConfiguration GetContainerConfiguration()
        {
            ConventionBuilder conventionBuilder = new ConventionBuilder();

            conventionBuilder.ForTypesMatching(type => type.FullName.EndsWith("Page"))
                             .Export(builder => builder.AsContractType<object>()
                                                       .AsContractName("OkraPage")
                                                       .AddMetadata("PageName", type => type.Name.Substring(0, type.Name.Length - 4)));

            conventionBuilder.ForTypesMatching(type => type.FullName.EndsWith("ViewModel"))
                             .Export(builder => builder.AsContractType<object>()
                                                       .AsContractName("OkraViewModel")
                                                       .AddMetadata("PageName", type => type.Name.Substring(0, type.Name.Length - 9)));

            return GetOkraContainerConfiguration()
                    .WithAssembly(typeof(AppBootstrapper).GetTypeInfo().Assembly, conventionBuilder);
        }
    }
}
